<?php
class UsersController extends AppController {

	var $name = 'Users';
	function beforeFilter() {
		parent::beforeFilter(); 
		$this->Auth->allow(array('*'));
	}
	function index() {
		$this->User->recursive = 0;
		$this->set('users', $this->paginate());
		
	}
	function find_list() {
		return $this->group->generatetreelist(null, null, null, '__');
	}
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('user', $this->User->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->User->create();
			$this->data['User']['password'] = $this->Auth->password($this->data['User']['password_new']);
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('Đã thêm user mới', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
		$groups = $this->User->Group->find('list');
		$this->set(compact('groups'));
	}
	function find_user_comment($user_id = null){
		return $this->User->read(null, $user_id);
	}
	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {//data[User][new_password]
			$new_pass = trim($this->data['User']['new_password']);
			if (!empty($new_pass)) {
				$this->data['User']['password'] = $this->Auth->password($new_pass);
			}
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The user has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->User->read(null, $id);
		}
		$groups = $this->User->Group->find('list');
		$this->set(compact('groups'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for user', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->User->delete($id)) {
			$this->Session->setFlash(__('User deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('User was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	function login() {
		$this->layout ='login';
		
		if (empty($this->data)) {
				$cookie = $this->Cookie->read('Auth.User');
				if (!is_null($cookie)) {
					if ($this->Auth->login($cookie)) {
						// Clear auth message, just in case we use it.
						$this->Session->delete('Message.auth');
						$this->redirect($this->Auth->redirect());
					} else { // Delete invalid Cookie
						$this->Cookie->delete('Auth.User');
						}
				}
			}
			
			if ($this->Auth->user()) {
				if (!empty($this->data) && $this->data['User']['remember_me']) {
					$cookie = array();
					$cookie['username'] = $this->data['User']['username'];
					$cookie['password'] = $this->data['User']['password'];
					$this->Cookie->write('Auth.User', $cookie, true, '+2 weeks');
					unset($this->data['User']['remember_me']);
				}
			   $this->Session->setFlash("Đăng nhập thành công");
			   $this->redirect($this->Auth->redirect());
			}  
   }
	
	function logout() {
		// $this->Session->setFlash("You are loged out!");
		$this->Auth->logout();
		$cookie = $this->Cookie->read('Auth.User');
		if (!is_null($cookie)) {
		$this->Cookie->delete('Auth.User');
		}
		//      $this->Session->setFlash("Đăng xuất thành công");
		$this->redirect('login');
	}

	function new_user() {
		$this->layout ='login';
		if (!empty($this->data)) {
			$this->User->create();
			$this->data['User']['group_id'] = '2';
			$this->data['User']['password'] = $this->data['User']['password_new'];
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('Đã thêm user mới', true));
				$this->redirect(array('action' => 'login'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
	}

	/**ACL
	----------------------------------------*/
	function initDB() {
		$group =& $this->User->Group;
		//cho phép administrators truy cập mọi thứ
		$group->id = 5;     
		$this->Acl->allow($group, 'controllers');

		//cho phép managers quản lý new, video,categories, cấm truy xuất khu vực và action khác
		$group->id = 6;
		$this->Acl->deny($group, 'controllers');
		$this->Acl->allow($group, 'controllers/news');
		$this->Acl->allow($group, 'controllers/videos');
		$this->Acl->allow($group, 'controllers/categories');  
		//cho phép users chỉ được add và edit news, categories, cấm truy xuất các khu vực và action khác
		$group->id = 7;
		$this->Acl->deny($group, 'controllers');        
		$this->Acl->allow($group, 'controllers/news/add');
		$this->Acl->allow($group, 'controllers/news/edit');  
		$this->Acl->allow($group, 'controllers/categories/add');  
		$this->Acl->allow($group, 'controllers/categories/edit');  
		$this->Session->setFlash(__('Đã phân quyền thành công', true));
		$this->redirect(array('action' => 'index'));
	}
}
